<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2018/7/23
 * Time: 14:52
 */
namespace app\jujin\controller;

use controller\JunjinAdmin;
use think\Db;

class AccountLog extends JunjinAdmin
{
    public $table='AccountLog';

    public function index()
    {
        $this->title = '会员流水';
        $get = $this->request->get();
        $db = Db::name($this->table);
        if (isset($get['addtime']) && $get['addtime'] !== '') {
            list($start, $end) = explode(' - ', $get['addtime']);
            $db->whereBetween('addtime', [strtotime("{$start} 00:00:00"), strtotime("{$end} 23:59:59")]);
        }
        if(isset($get['type'])&&$get['type']!=''){
            $db->where('type',$get['type']);
        }
        if(isset($get['mid'])&&$get['mid']!=''){
            $db->where('mid',$get['mid']);
        }
        if(isset($get['mobile'])&&$get['mobile']!=''){
            $mid=Db::name('store_member')
                ->where('phone',$get['mobile'])
                ->value('id');
            $db->where('mid',$mid);
        }
        $this->_assign_data();
        return parent::_list($db->order('id desc'));
    }

    protected function _form_filter(&$data,$extend_data)
    {
        if($this->request->isGet()){
            $this->title='资金调整';
            $this->_assign_data();
        }else{
            $validate=new \app\jujin\validate\AccountLog();
            if(false === $validate->check($data))$this->error($validate->getError());
            $data['type']=5;
            $res=log_account_change($data['mid'],$data['money'],0,$data['remark'],$data['type']);
            if($res){
                list($base, $spm, $url) = [url('@admin'), $this->request->get('spm'), url(request()->module().'/'.request()->controller().'/index')];
                $this->success('数据保存成功！', "{$base}#{$url}?spm={$spm}");
            }else{
                $this->error('操作失败,请重试');
            }
        }
    }

    private function _assign_data(){
        //获取所有用户
        $members=Db::name('store_member')->where('status',1)->field('id,nickname')->select();
        $this->assign('members',$members);
    }

}